<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let arr = [1,2,3] // 可迭代对象
        console.log('迭代器工厂函数', arr[Symbol.iterator])
        let iter = arr[Symbol.iterator]() // 迭代器
        console.log(iter)
        // 执行迭代器
        console.log(iter.next()) // {value: 1, done: false}
        console.log(iter.next()) // {value: 2, done: false}
        console.log(iter.next()) // {value: 3, done: false}
        console.log(iter.next()) // {value: undefined, done: true}

        // Foo实现了可迭代接口Iterable
        // 调用默认的迭代器工厂函数会返回一个实现迭代器接口Iterator的迭代器对象
        class Foo {
            [Symbol.iterator] () {
                return {
                    next() {
                        return {done: false, value: 'foo'}
                    }
                }
            }
        }
        let foo = new Foo()
        console.log(foo[Symbol.iterator]())  // {next: ƒ}

    </script>
</body>
</html>